﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Infrastructure;
using Xierun.Dtos;
using Microsoft.AspNetCore.Mvc;
using Xierun.IServices;
using Xierun.Models;

namespace Xierun.WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly IBase_UserService _accountService;
        private IMapper _mapper;

        public UserController(IBase_UserService accountService,IMapper mapper)
        {
            _accountService = accountService;
            _mapper = mapper;
        }
        

        /// <summary>
        /// 根据条件查询数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<DtoResult<IEnumerable<Base_UserDto>>> Get()
        {
            var models = await _accountService.GetListAsync(null);
            var modelResource = _mapper.Map<IEnumerable<Base_User>, IEnumerable<Base_UserDto>>(models);
            var result = new DtoResult<IEnumerable<Base_UserDto>>
            {
                Result = modelResource
            };
            return result;
        }

        /// <summary>
        /// 根据id获取User对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<DtoResult<Base_UserDto>> Get(string id)
        {
            var result = new DtoResult<Base_UserDto>();
            var model = await _accountService.GetSingleByIdAsync(id);
            if (model == null)
            {
                result.Code = 400;
                result.Message = "请输入正确的id";
            }

            var modelResource = _mapper.Map<Base_User, Base_UserDto>(model);
            result.Result = modelResource;

            return result;
        }

        /// <summary>
        /// 增加新用户
        /// </summary>
        /// <param name="userDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<DtoResult<bool>> Post([FromBody] Base_UserDto userDto)
        {
            var result = new DtoResult<bool>();
            if (await _accountService.IsExistAsync(x => x.Account == userDto.Account))
            {
                result.Code = 400;
                result.Message = "用户已存在";
                return result;
            }
            var user = _mapper.Map<Base_UserDto, Base_User>(userDto);
            bool isOk= await _accountService.AddAsync(user);
            result.Result = isOk;

            return result;

        }

        /// <summary>
        /// 根据id修改用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="userDto"></param>
        [HttpPut("{id}")]
        public async Task<DtoResult<bool>> Put(string id, [FromBody] Base_UserDto userDto)
        {
            var result = new DtoResult<bool>();
            var user = await _accountService.GetSingleByIdAsync(id);
            if (user == null)
            {
                result.Code = 400;
                result.Message = "请输入正确的id";
                return result;
            }

            _mapper.Map(userDto, user);
            bool isOk= await _accountService.UpdateAsync(user);
            result.Result = isOk;

            return result;

        }

        /// <summary>
        /// 根据id修改用户部分信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="userDto"></param>
        [HttpPatch("{id}")]
        public async Task<DtoResult<bool>> Patch(string id, [FromBody] Base_UserDto userDto)
        {

            var result = new DtoResult<bool>();
            var user = await _accountService.GetSingleByIdAsync(id);
            if (user == null)
            {
                result.Code = 400;
                result.Message = "请输入正确的id";
                return result;
            }


            _mapper.Map(userDto, user);
            //user.ModifyUserName = "4";
            //只更新Account，ModifyUserName
            bool isOk = await _accountService.UpdateAsync(user, true, new List<string> { "Account", "ModifyUserName1" });
            result.Result = isOk;

            return result;

        }


        /// <summary>
        /// 根据id删除实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<DtoResult<bool>> Delete(string id)
        {
            var result = new DtoResult<bool>();
            var user = await _accountService.GetSingleByIdAsync(id);
            if (user == null)
            {
                result.Code = 400;
                result.Message = "请输入正确的id";
                return result;
            }

            bool isOk = await _accountService.DeleteByIdAsync(id);
            result.Result = isOk;
            return result;
        }
    }
}
